﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaiTap1
{
    class BieuThucChia : BieuThuc
    {
        #region - Attribute
        private BieuThuc _bieuThuc1;
        private BieuThuc _bieuThuc2;
        #endregion

        #region - Property
        public BieuThuc BieuThuc1
        {
            get { return _bieuThuc1; }
            set { _bieuThuc1 = value; }
        }
        public BieuThuc BieuThuc2
        {
            get { return _bieuThuc2; }
            set { _bieuThuc2 = value; }
        }
        #endregion

        #region - Method
        public BieuThucChia()
        { 
        
        }
        public BieuThucChia(BieuThuc bieuThuc1, BieuThuc bieuThuc2)
        {
            _bieuThuc1 = bieuThuc1;
            _bieuThuc2 = bieuThuc2;
        }
        public override string TinhDaoHam()
        {
            string ketQua = "(" + _bieuThuc1.TinhDaoHam() + " * " + _bieuThuc2 + " - " + _bieuThuc1 + " * " + _bieuThuc2.TinhDaoHam() + ") / "
                + _bieuThuc2.ToString() + "^2";
            return "(" + ketQua + ")";
        }
        public override double TinhGiaTri(double x)
        {
            return _bieuThuc1.TinhGiaTri(x) / _bieuThuc2.TinhGiaTri(x);
        }
        public void GanBieuThuc(BieuThuc bieuThuc)
        {
            if (_bieuThuc1 == null)
                _bieuThuc1 = bieuThuc;
            else
                _bieuThuc2 = bieuThuc;
        }
        public override string ToString()
        {
            string ketQua = string.Empty;
            //Một trong hai biểu thức là 1
            if (_bieuThuc1.ToString() == "1")
                ketQua = _bieuThuc1.ToString();
            else if (_bieuThuc2.ToString() == "1")
                ketQua = _bieuThuc1.ToString();
            else if (_bieuThuc1.ToString() == "" || _bieuThuc2.ToString() == "") //Một trong hai biểu thức là 0
                ketQua = "";
            else
                ketQua = _bieuThuc1.ToString() + " / " + _bieuThuc2.ToString();
            return ketQua;
        }
        #endregion
    }
}
